package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.ClientInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.ClientInfo;
import com.yaojiaxun.logisticsPlatformSystem.mapper.ClientInfoMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.MD5Util;
import com.yaojiaxun.logisticsPlatformSystem.util.SnowFlakeIDWorker;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2019/2/12 13:54
 * @Description:
 */
@Service
public class ClientInfoService {
    @Autowired
    private ClientInfoMapper clientInfoMapper;

    public ClientInfo findClientById(Long clientId){
        return clientInfoMapper.selectClientById(clientId);
    }

    public ClientInfoDTO beanToDTO(ClientInfo clientInfo){
        if (clientInfo == null)
            return null;
        ClientInfoDTO clientInfoDTO = new ClientInfoDTO();
        BeanUtils.copyProperties(clientInfo, clientInfoDTO);
        clientInfoDTO.setCreateTime(TimeFormat.convertTime(clientInfo.getCreateTime()));
        clientInfoDTO.setUpdateTime(TimeFormat.convertTime(clientInfo.getUpdateTime()));
        return clientInfoDTO;
    }

    public PageDTO<ClientInfoDTO> findPage(Integer pageNum){
        PageDTO<ClientInfoDTO> pageDTO = new PageDTO<>();
        Page<ClientInfoDTO> page = new Page<>(pageNum, 10);
        List<ClientInfoDTO> results = clientInfoMapper.selectPage(page, null).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setPage(page);
        pageDTO.setResult(results);
        return pageDTO;
    }

    public void deleteClientById(Long clientId){
        clientInfoMapper.deleteClientById(clientId);
    }

    public void insertClientInfo(ClientInfo clientInfo){
        clientInfo.setClientId(new SnowFlakeIDWorker(8, 1).nextId());
        clientInfo.setCreateTime(Instant.now().toEpochMilli());
        clientInfo.setUpdateTime(Instant.now().toEpochMilli());
        clientInfo.setClientPassword(MD5Util.inputPassToDbPass(clientInfo.getClientPassword()));
        clientInfoMapper.insertClientInfo(clientInfo);
    }

    public Boolean updateClientInfo(ClientInfo clientInfo){
        clientInfo.setUpdateTime(Instant.now().toEpochMilli());
        return clientInfoMapper.updateClientInfo(clientInfo)>0;
    }

    public List<ClientInfo> clientSearch(String tiaojian){
        ClientInfo clientInfo = clientInfoMapper.findClientInfoByAccount(tiaojian);
        List<ClientInfo> list2 = clientInfoMapper.findClientInfoByPhone(tiaojian);
        List<ClientInfo> results = new ArrayList<>();
        results.add(clientInfo);
        results.addAll(list2);
        return results;
    }

    public ClientInfo findClientByClientAccount(String clientAccount){
        return clientInfoMapper.findClientInfoByAccount(clientAccount);
    }
    public ClientInfoDTO findClientByClientAccountTimeFormat(String clientAccount){
        ClientInfo clientInfo = clientInfoMapper.findClientInfoByAccount(clientAccount);
        ClientInfoDTO clientInfoDTO = new ClientInfoDTO();
        BeanUtils.copyProperties(clientInfo, clientInfoDTO);
        clientInfoDTO.setCreateTime(TimeFormat.convertTime(clientInfo.getCreateTime()));
        return clientInfoDTO;
    }
}
